package com.hxd.otc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.widget.AutoSplitOnTvGlobalLayoutAdapter;

/**
 * 包  名 :  com.hxd.otc.widget
 * 类  名 :  SingBlocksDialog
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/6 19:44
 * 描  述 :  ${但点击按钮Dialog 如果不要内容栏，不设定内容文字就好}
 */
public class SingBlocksDialog extends Dialog implements View.OnClickListener {

    private TextView mTitleText;
    private TextView mContentText;
    private TextView mGotItText;
    private LinearLayout mTitleParentGroup;

    public void setOnSingClickListener(OnSingClickListener onSingClickListener) {
        this.onSingClickListener = onSingClickListener;
    }

    private OnSingClickListener onSingClickListener;


    public SingBlocksDialog(@NonNull Context context) {
        this(context, R.style.BlockDialog);

    }

    public SingBlocksDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.weight_sing_blocks_dialog);
        initView();
        initWindows();
    }

    private void initView() {
        mTitleText = findViewById(R.id.tv_title);
        mContentText = findViewById(R.id.content_tv);
        mGotItText = findViewById(R.id.got_it_tv);
        mTitleParentGroup = findViewById(R.id.title_ll);
        mGotItText.setOnClickListener(this);
    }

    private void initWindows() {
        setCanceledOnTouchOutside(false);
        Window window = getWindow();
        if (window != null) {
            DisplayMetrics dm = new DisplayMetrics();
            window.getWindowManager().getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams windowAttributes = window.getAttributes();
            windowAttributes.width = WindowManager.LayoutParams.MATCH_PARENT;
            windowAttributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            windowAttributes.gravity = Gravity.CENTER;
            window.setAttributes(windowAttributes);
        }
    }


    public SingBlocksDialog setTitle(@NonNull String title) {
        mTitleText.setText(title);
        return this;
    }

    public SingBlocksDialog setContent(@NonNull String content) {
        mContentText.setText(content);
        mContentText.getViewTreeObserver().addOnGlobalLayoutListener(new AutoSplitOnTvGlobalLayoutAdapter(mContentText));
        return this;
    }

    public SingBlocksDialog setContent(@NonNull String content, int size) {
        mContentText.setText(content);
        mContentText.setTextSize(size);
        mContentText.getViewTreeObserver().addOnGlobalLayoutListener(new AutoSplitOnTvGlobalLayoutAdapter(mContentText));
        return this;
    }

    public SingBlocksDialog setContentGravityCenter(int gravity) {
        mContentText.setGravity(gravity);
        return this;
    }

    public SingBlocksDialog setGotText(@NonNull String content) {
        mGotItText.setText(content);
        return this;
    }

    public SingBlocksDialog setTitleGone() {
        mTitleText.setVisibility(View.GONE);
        return this;
    }

    public SingBlocksDialog setGotText(@NonNull String content, View.OnClickListener listener) {
        mGotItText.setText(content);
        mGotItText.setOnClickListener(listener);
        return this;
    }

    @Override
    public void show() {
        mTitleParentGroup.setVisibility(TextUtils.isEmpty(mContentText.getText()) ? View.GONE : View.VISIBLE);
        super.show();
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (onSingClickListener != null) {
            onSingClickListener.singItemClick();
        }
    }

    public interface OnSingClickListener {
        void singItemClick();
    }


}
